#include <QDebug>

#include "activitytabitemwidget.h"
#include "bizlogic/cvactivitymanager.h"
#include "widgets/moreactionwidget.h"

ActivityTabItemWidget::ActivityTabItemWidget(QWidget *parent) :
    CVTabItem(parent)
{
    connect(this->refresh, SIGNAL(clicked()), this, SLOT(loadActivities()));
}

void ActivityTabItemWidget::loadActivities()
{
    qDebug() << "==ActivityTabItemWidget::loadActivities==";
    CVActivityManager *manager = new CVActivityManager();
    QMap<QString, QString> map;
    map.insert("userId", "guang-hui.li@broadvision.com");
    manager->getActivities(map);
    connect(manager, SIGNAL(handActivities(QList<CVActivity*>,QString)),
            this, SLOT(setActivities(QList<CVActivity*>,QString)));
}

void ActivityTabItemWidget::reload(bool b)
{
    if (b)
    {
        loadActivities();
    }
}

void ActivityTabItemWidget::load()
{
    loadActivities();
}

void ActivityTabItemWidget::setActivities(QList<CVActivity *> list, const QString &message)
{
    qDebug() << "==ActivityTabItemWidget::setActivities==";
    this->getList()->clear();
    int size = list.size();
    if (size != 0)
    {
        QList<QString> *guids = new QList<QString>();
        for (int i = 0; i < size; ++i)
        {
            CVActivity *activity = list.at(i);
            guids->append(activity->getObjectId());
            CVActivityWidget *widget = new CVActivityWidget(activity);
            this->getList()->appendItem(widget);
        }

        //CVActivity *activity = list.at(size - 1);
        //qDebug() << "river id: " << activity->getRiverId();
        MoreActionWidget *mWidget = new MoreActionWidget();
        this->getList()->appendItem(mWidget);
    }
    else
    {
        MoreActionWidget *mWidget = new MoreActionWidget();
        mWidget->setDisplayText("No activities found");
        this->getList()->appendItem(mWidget);
    }
    QWidget *nativeWidget = this->getList()->widget();
    qDebug() << nativeWidget->objectName();
}
